<template>
  <div class="movie-detail">
    <div class="back-button">
      <el-button type="primary" @click="goBack" circle>
        <el-icon><arrow-left /></el-icon>
      </el-button>
    </div>
    
    <div class="detail-container" v-if="movie">
      <div class="poster-section">
        <img :src="movie.picPaths || placeholderImage" :alt="movie.title" class="main-poster">
      </div>
      
      <div class="info-section">
        <h1>{{ movie.title }}</h1>
        
        <div class="meta-info">
          <div class="meta-item">
            <el-icon><calendar /></el-icon>
            <span>{{ formattedDate }}</span>
          </div>
          <div class="meta-item">
            <el-icon><user /></el-icon>
            <span>{{ movie.actors || '未知演员' }}</span>
          </div>
          <div class="meta-item">
            <el-icon><star /></el-icon>
            <el-rate v-model="rating" disabled show-score text-color="#ff9900" />
          </div>
        </div>
        
        <div class="description">
          <h2>剧情简介</h2>
          <p>{{ movie.desc || '暂无剧情简介' }}</p>
        </div>
        
        <div class="comment-section">
          <h2>用户评价</h2>
          
          <div class="comment-form" v-if="userStore.isLoggedIn">
            <el-input
              v-model="commentContent"
              :rows="3"
              type="textarea"
              placeholder="写下你的观后感..."
            />
            <el-button type="primary" @click="submitComment" :loading="commentLoading">
              发表评论
            </el-button>
          </div>
          <div v-else class="login-prompt">
            请<router-link to="/login">登录</router-link>后发表评论
          </div>
          
          <div class="comments-list">
            <div v-if="comments.length === 0" class="no-comments">
              暂无评论，快来发表第一条评论吧！
            </div>
            
            <div v-for="comment in comments" :key="comment.id" class="comment-item">
              <div class="comment-header">
                <span class="username">{{ comment.username }}</span>
                <span class="comment-time">{{ formatTime(comment.createdAt) }}</span>
              </div>
              <div class="comment-content">
                {{ comment.body }}
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <div v-else class="loading-container">
      <el-skeleton :rows="5" animated />
    </div>
  </div>
</template>

<script>
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useUserStore } from '../../stores/user'
import { movieApi, commentApi } from '../../utils/api'
import { 
  ArrowLeft, 
  Calendar, 
  User, 
  Star 
} from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

export default {
  components: { ArrowLeft, Calendar, User, Star },
  setup() {
    const route = useRoute()
    const router = useRouter()
    const userStore = useUserStore()
    
    const movie = ref(null)
    const comments = ref([])
    const commentContent = ref('')
    const commentLoading = ref(false)
    const placeholderImage = 'https://via.placeholder.com/500x750?text=No+Poster'
    
    const rating = computed(() => {
      return movie.value?.rating ? parseFloat(movie.value.rating) / 2 : 3.5
    })
    
    const formattedDate = computed(() => {
      if (!movie.value?.addedTime) return '未知日期'
      return new Date(movie.value.addedTime).toLocaleDateString()
    })
    
    const fetchMovie = async () => {
      try {
        const response = await movieApi.getSingle(route.params.id)
        movie.value = response.data
      } catch (error) {
        ElMessage.error('获取电商详情失败')
      }
    }
    
    const fetchComments = async () => {
      try {
        const response = await commentApi.getList(route.params.id)
        comments.value = response.data
      } catch (error) {
        ElMessage.error('获取评论失败')
      }
    }
    
    const submitComment = async () => {
      if (!commentContent.value.trim()) {
        ElMessage.warning('评论内容不能为空')
        return
      }
      
      commentLoading.value = true
      try {
        await commentApi.create({
          articleid: route.params.id,
          body: commentContent.value,
          userid: userStore.user.id,
          replyType: '电商'
        })
        ElMessage.success('评论发表成功')
        commentContent.value = ''
        await fetchComments()
      } catch (error) {
        ElMessage.error('评论发表失败')
      } finally {
        commentLoading.value = false
      }
    }
    
    const formatTime = (timeString) => {
      if (!timeString) return ''
      return new Date(timeString).toLocaleString()
    }
    
    const goBack = () => {
      router.go(-1)
    }
    
    onMounted(() => {
      fetchMovie()
      fetchComments()
    })
    
    return {
      movie,
      comments,
      commentContent,
      commentLoading,
      placeholderImage,
      rating,
      formattedDate,
      userStore,
      submitComment,
      formatTime,
      goBack
    }
  }
}
</script>

<style scoped>
.movie-detail {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  position: relative;
}

.back-button {
  position: absolute;
  top: 20px;
  left: 20px;
  z-index: 10;
}

.detail-container {
  display: grid;
  grid-template-columns: 300px 1fr;
  gap: 30px;
  margin-top: 20px;
}

.poster-section {
  position: sticky;
  top: 20px;
}

.main-poster {
  width: 100%;
  border-radius: 8px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}

.info-section h1 {
  font-size: 2rem;
  margin-bottom: 20px;
  color: var(--dark-bg);
}

.meta-info {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 1px solid #eee;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 5px;
  color: #666;
}

.description {
  margin-bottom: 40px;
}

.description h2 {
  font-size: 1.5rem;
  margin-bottom: 15px;
  color: var(--dark-bg);
}

.description p {
  line-height: 1.8;
  color: #555;
}

.comment-section h2 {
  font-size: 1.5rem;
  margin-bottom: 20px;
  color: var(--dark-bg);
}

.comment-form {
  margin-bottom: 30px;
}

.comment-form .el-button {
  margin-top: 10px;
}

.login-prompt {
  margin-bottom: 30px;
  padding: 15px;
  background-color: #f5f5f5;
  border-radius: 4px;
  text-align: center;
}

.login-prompt a {
  color: var(--primary-color);
  text-decoration: none;
}

.comments-list {
  border-top: 1px solid #eee;
  padding-top: 20px;
}

.no-comments {
  text-align: center;
  padding: 20px;
  color: #999;
}

.comment-item {
  padding: 15px 0;
  border-bottom: 1px solid #f0f0f0;
}

.comment-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
  font-size: 0.9rem;
}

.username {
  font-weight: bold;
  color: var(--dark-bg);
}

.comment-time {
  color: #999;
}

.comment-content {
  line-height: 1.6;
}

.loading-container {
  padding: 50px;
}

@media (max-width: 768px) {
  .detail-container {
    grid-template-columns: 1fr;
  }
  
  .poster-section {
    position: static;
    margin-bottom: 20px;
  }
  
  .main-poster {
    max-width: 250px;
    margin: 0 auto;
    display: block;
  }
}
</style>